# Name: PackwithPyInstaller.py #
# 
# Author: Zhang Huiyuan #
# Creation Date: 2021.11.17 #
# Version: v0.0.2 #
# 

import os

import GlobalVariables

# CMD: pyinstaller.exe -F -w -i '.\Icon.ico' -n 'SoftwareName_v0.0.1' '.\Main.py'
def CreatePyInstallerPackingCMD():
	# Icon path
	szIconPath = GetSoftwareIcon()
	if(szIconPath):
		pass
	else:
		print("Cannot find icon.\nPlease check './Icons/'.\n")
		return 0
	# Target '.spec' name
	szTargetSPECName = '%s_%s' %(g_szSoftwareName, g_szVersionNum)
	# Source '.py' name
	szSourcePYName = '%s/Main.py' %(os.getcwd())
	# Version file name
	szVersionFile = '%s/version.txt' %(os.getcwd())
	if(os.path.exists(szVersionFile)):
		pass
	else:
		# print("Cannot find version file.\nPlease check './version.txt'.\n")
		return 0

	szPyInstallerPackCMD = 'pyinstaller -F -w --version-file %s -i %s -n %s %s' %(szVersionFile, szIconPath, szTargetSPECName, szSourcePYName)
	return szPyInstallerPackCMD

def GetSoftwareIcon():
	try:
		temp_lsIconsPath = os.listdir('%s/Icons' %(os.getcwd()))
	except Exception as e:
		print('%s\n' %e)
		return 0
	if(temp_lsIconsPath):
		szTargetIconPath = '%s/Icons/%s' %(os.getcwd(), temp_lsIconsPath[0])
		# print('Use icon [%s]\n' %szTargetIconPath)
		return szTargetIconPath
	else:
		# print("Cannot find icon.\nPlease check './Icons/'.\n")
		return 0
	return 0

def CreateVersionFile():
	iFlagCheckVersionFile = 0
	lszVersionFileText = [
							'# UTF-8', 
							'VSVersionInfo(', 
							'ffi=FixedFileInfo(', 
							'filevers=%s,' %g_szFileevers, # 文件版本，系统检测版本主要依据
							'prodvers=%s,' %g_szFileevers, # 产品版本
							'mask=0x3f,', # 掩码
							'flags=0x0,', # 标志
							'OS=0x4,', # 此文件的操作系统，0x4-NT
							'fileType=0x1,', # 文件的常规类型，0x1-该文件是一个应用程序
							'subtype=0x0,', # 文件的功能，0x0表示该文件类型未定义
							'date=(0, 0)', # 创建日期和时间戳
							'),', 
							'  kids=[', 
							'StringFileInfo(', 
							'  [', 
							'  StringTable(', 
							'    u\'040904B0\',', # 0x409表示英文，0x04B0表示Unicode
							'    [StringStruct(u\'CompanyName\', u\'%s\'),' %g_szCompanyName, # 公司名称，鼠标悬浮exe会显示
							'    StringStruct(u\'FileDescription\', u\'%s\'),' %g_szFileDescription, # 文件说明，鼠标悬浮exe会显示，也会显示在 详细信息-文件说明
							'    StringStruct(u\'FileVersion\', u\'%s\'),' %g_szFileVersion, # 文件版本
							'    StringStruct(u\'InternalName\', u\'%s\'),' %g_szInternalName, # 内部名称
							'    StringStruct(u\'LegalCopyright\', u\'%s\'),' %g_szLegalCopyright, # 版权，会显示在 详细信息-版权
							'    StringStruct(u\'OriginalFilename\', u\'%s\'),' %g_szOriginalFilename, # 原始文件名，会显示在 详细信息-原始文件名
							'    StringStruct(u\'ProductName\', u\'%s\'),' %g_szProductName, # 产品名称，会显示在 详细信息-产品名称
							'    StringStruct(u\'ProductVersion\', u\'%s\')])' %g_szProductVersion, # 产品版本，会显示在 详细信息-产品版本
							'  ]),', 
							'VarFileInfo([VarStruct(u\'Translation\', %s)])' %g_szTranslation,  # 2052（0x804）简体中文, 1200（0x4B0）Unicode
							'  ]', 
							')'
							]
	try:
		with open('./version.txt', 'w+', encoding = 'utf-8') as temp_obFileWriter:
			temp_obFileWriter.write('\n'.join(lszVersionFileText))
	except Exception as e:
		print(e)
	else:
		iFlagCheckVersionFile = 1

	return iFlagCheckVersionFile


if __name__ == '__main__':
	globals().update(GlobalVariables.GetAllCustomGlobalVariables())

	if(CreateVersionFile()):
		szPyInstallerPackCMD = CreatePyInstallerPackingCMD()
		print('Current Version: %s_%s\n' %(g_szSoftwareName, g_szVersionNum))
		szCMDLine = CreatePyInstallerPackingCMD()
		print(szCMDLine if szCMDLine else '')
		print(os.system(szCMDLine) if szCMDLine else "Cannot build CMD lines.\n")
	else:
		print("Cannot find version file.\nPlease check './version.txt'.\n")